package com.tommy.store.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileDownloader {
    public static String downloadFile(String remoteFilePath, String localFilePath) {
        try {
            URL url = new URL(remoteFilePath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
            File localFile = new File(localFilePath);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
            bis.close();
            bos.close();
            connection.disconnect();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public static void main(String[] args) {
        String remoteFilePath = "https://cn-beijing-data.aliyundrive.net/Vs3GjX7R%2F230830090%2F67106c34d356ccb8b261431da77c02b7363ef566%2F67106c34d2bebfa2a332460db18adff4ed3321c1?callback=eyJjYWxsYmFja1VybCI6Imh0dHA6Ly9iajI5LmFwaS1ocC5hbGl5dW5wZHMuY29tL3YyL2ZpbGUvZG93bmxvYWRfY2FsbGJhY2siLCJjYWxsYmFja0JvZHkiOiJodHRwSGVhZGVyLnJhbmdlPSR7aHR0cEhlYWRlci5yYW5nZX1cdTAwMjZidWNrZXQ9JHtidWNrZXR9XHUwMDI2b2JqZWN0PSR7b2JqZWN0fVx1MDAyNmRvbWFpbl9pZD0ke3g6ZG9tYWluX2lkfVx1MDAyNnVzZXJfaWQ9JHt4OnVzZXJfaWR9XHUwMDI2ZHJpdmVfaWQ9JHt4OmRyaXZlX2lkfVx1MDAyNmZpbGVfaWQ9JHt4OmZpbGVfaWR9XHUwMDI2cGRzX3BhcmFtcz0ke3g6cGRzX3BhcmFtc31cdTAwMjZ2ZXJzaW9uPSR7eDp2ZXJzaW9ufSIsImNhbGxiYWNrQm9keVR5cGUiOiJhcHBsaWNhdGlvbi94LXd3dy1mb3JtLXVybGVuY29kZWQiLCJjYWxsYmFja1N0YWdlIjoiYmVmb3JlLWV4ZWN1dGUiLCJjYWxsYmFja0ZhaWx1cmVBY3Rpb24iOiJpZ25vcmUifQ%3D%3D&callback-var=eyJ4OmRvbWFpbl9pZCI6ImJqMjkiLCJ4OnVzZXJfaWQiOiIzMmJhZjQzYmRiNmE0Mzg4OGY0NGMwNzU3NzYyNmQzNyIsIng6ZHJpdmVfaWQiOiIyMzA4MzAwOTAiLCJ4OmZpbGVfaWQiOiI2NzEwNmMzNGQzNTZjY2I4YjI2MTQzMWRhNzdjMDJiNzM2M2VmNTY2IiwieDpwZHNfcGFyYW1zIjoie1wiYXBcIjpcIjNkOWQ3Zjk5ZTU2NjQ3YWI4ZmFkOTVlYTg3MTNhZTI4XCJ9IiwieDp2ZXJzaW9uIjoidjMifQ%3D%3D&di=bj29&dr=230830090&f=67106c34d356ccb8b261431da77c02b7363ef566&pds-params=%7B%22ap%22%3A%223d9d7f99e56647ab8fad95ea8713ae28%22%7D&response-content-disposition=attachment%3B%20filename%2A%3DUTF-8%27%27lowcode_data.db&security-token=CAISvgJ1q6Ft5B2yfSjIr5b3Id78i5JrgabdW3bitE5nSvkduLPF0Dz2IHhMf3NpBOkZvvQ1lGlU6%2Fcalq5rR4QAXlDfNS%2BiIgeWq1HPWZHInuDox55m4cTXNAr%2BIhr%2F29CoEIedZdjBe%2FCrRknZnytou9XTfimjWFrXWv%2Fgy%2BQQDLItUxK%2FcCBNCfpPOwJms7V6D3bKMuu3OROY6Qi5TmgQ41Uh1jgjtPzkkpfFtkGF1GeXkLFF%2B97DRbG%2FdNRpMZtFVNO44fd7bKKp0lQLs0ARrv4r1fMUqW2X543AUgFLhy2KKMPY99xpFgh9a7j0iCbSGyUu%2FhcRm5sw9%2Byfo34lVYne0xUBQweCi4IClLcc%2BmqdsRIvJzWstJ7Gf9LWqChvSgk4TxhhcNFKSTQrInFCB0%2BcRObJl16iIyeqgvXtuMkagAFzTNpCuGaKcYvO6J9M8cH%2FE8No1zEuoLyg9y83PZVsvjwIBUJWasyPQNr1P5gst%2BCl6wjc3PCJFcpGrIB87GG%2FGVdNtPCg1ugeqVjo6LVZQng4RBRGFALrEP%2Br0qAciNPF0F%2FfxViB41gdnutXQTAsOiwo6jVU%2B9fW%2FjoN3cbyuCAA&u=32baf43bdb6a43888f44c07577626d37&x-oss-access-key-id=STS.NUBjdHfMJ6d6YPSPN2Fv1Wvn2&x-oss-expires=1729130764&x-oss-signature=DoQ2OwrJ3F%2FSLTyLnaCPoqZ1JhleYfrXKpKl%2F03BgLc%3D&x-oss-signature-version=OSS2"; // 远程文件URL
        String localFilePath = System.getProperty("user.home") + "/lowcode_data.db"; // 本地文件路径，存储在用户根目录下
        downloadFile(remoteFilePath, localFilePath);
        System.out.println("文件下载完成！");
    }
}
